import api from '../utils/api';

// 紧急呼叫类型定义
export interface EmergencyCall {
  id: number;
  userId: number;
  userName: string;
  phone: string;
  location: string;
  reason: string;
  status: number; // 1-未处理，2-处理中，3-已完成，4-已取消
  handlerId: number;
  handlerName: string;
  remark: string;
  callTime: string;
  handleTime: string;
  createTime: string;
  updateTime: string;
}

// 处理紧急呼叫请求参数
export interface HandleEmergencyCallRequest {
  handlerId: number;
  handlerName: string;
  remark: string;
}

// 紧急呼叫服务类
class EmergencyCallService {
  // 发起紧急呼叫
  async createEmergencyCall(params: {
    userId: number;
    location: string;
    reason: string;
  }): Promise<EmergencyCall> {
    return await api.post('/emergency/create', params);
  }

  // 处理紧急呼叫
  async handleEmergencyCall(id: number, params: HandleEmergencyCallRequest): Promise<EmergencyCall> {
    return await api.put(`/emergency/handle/${id}`, params);
  }

  // 取消紧急呼叫
  async cancelEmergencyCall(id: number): Promise<EmergencyCall> {
    return await api.put(`/emergency/cancel/${id}`);
  }

  // 获取未处理的紧急呼叫
  async getUnhandledCalls(): Promise<EmergencyCall[]> {
    return await api.get('/emergency/unhandled');
  }

  // 获取用户的紧急呼叫历史
  async getUserCallHistory(userId: number): Promise<EmergencyCall[]> {
    return await api.get(`/emergency/history/${userId}`);
  }

  // 获取所有紧急呼叫记录（管理员用）
  async getAllCallRecords(page: number = 1, size: number = 10): Promise<any> {
    return await api.get('/emergency/list', {
      page,
      size
    });
  }

  // 获取紧急呼叫详情
  async getEmergencyCallDetail(id: number): Promise<EmergencyCall> {
    return await api.get(`/emergency/detail/${id}`);
  }
}

export default new EmergencyCallService();